package com.nkzy.controller;

import com.nkzy.pojo.Result;
import com.nkzy.pojo.User;
import com.nkzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable Integer id) {
        User user = userService.selectById(id);
        if (user != null) {
            return Result.success(user);
        } else {
            return Result.error("User not found");
        }
    }

    @GetMapping
    public Result<List<User>> getAllUsers() {
        List<User> users = userService.selectAll();
        return Result.success(users);
    }

    @PostMapping
    public Result<Void> addUser(@RequestBody User user) {
        userService.insert(user);
        return Result.success(null);
    }

    @PutMapping
    public Result<Void> updateUser(@RequestBody User user) {
        userService.update(user);
        return Result.success(null);
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.success(null);
    }
}